package core

import (
	"encoding/json"
	"io/ioutil"
	"os"
)

// 根据channel.json配置文件初始化Manager实例的channelPair字段
func Load_channels() []*Channel {
	var channels = make([]*Channel, 0)
	file, err := os.Open("./channel.json")
	if err != nil {
		panic(err)
	}
	defer file.Close()
	jsonData, err := ioutil.ReadAll(file)
	if err != nil {
		G.Log.Fatal("Load Channels Error", err)
	}
	cc := channelFile{}
	json.Unmarshal(jsonData, &cc)
	for _, ch := range cc.Channels {
		channels = append(channels,
			&Channel{config: ChannelConfig{ChannelCode: ch.ChannelCode, MediaPath: ch.MediaPath,
				Address: ch.Address, Username: ch.Username, Password: ch.Password, ChNo: ch.ChNo}},
		)
	}
	return channels
}
